import { defineStore } from "pinia";
import { userApi } from "@/api";
import { showNotify } from "vant";
import { router } from "@/modules/router";
import { removeToken } from '@/modules/auth';

export const UserStore = defineStore("user", {
  state: () => {
    return {
      userInfo: {},
      curIndex: 0,
      cateList: [],
      cateId: 0,
    };
  },
  persist: true,
  actions: {
    async getUserInfo() {
      const { code, data, msg } = await userApi.userInfo();
      if (code == 20000) {
        this.userInfo = data;
      } else {
        if (code == 401) {
          removeToken()
          router.push({ name: "login"})
        }
      }
    },
  },
  getters: {},
});
